<?php
/**
 * ---------------------------------------------------------
 * Gestion des services 
 * ---------------------------------------------------------
 * @author PoLuX
 * ---------------------------------------------------------
 * @static
 * @copyright PoLuX
 * @access public
 * @category Services
 */
class ServicesManagement {
	/**
	 * Relative path from application root to services directory
	 * @var string
	 */
	const C_S_DIRECTORY_SERVICES = 'services/';
	/**
	 * Classname prefix for services class
	 * @var string
	 */
	const C_S_CLASSNAME_PREFIX = 'Service_';
	/**
	 * Return true if service is identified successfully
	 * @param string $p_s_serviceName	Service name
	 * @return boolean
	 */
	public static function serviceExists($p_s_serviceName)
	{
		//echo "Testing existance of '".ServicesManagement::C_S_DIRECTORY_SERVICES.$p_s_serviceName.".php"."'".'\n';
		return  file_exists(ServicesManagement::C_S_DIRECTORY_SERVICES.$p_s_serviceName.".php");
	}
	/**
	 * Return class name with prefix (for internal identification)
	 * @param string $p_s_serviceName	Service name
	 * @return string
	 */
	public static function getClassName($p_s_serviceName)
	{
		return ServicesManagement::C_S_CLASSNAME_PREFIX.$p_s_serviceName;
	}
	/**
	 * Return Service class filepath
	 * @param string $p_s_serviceName
	 * @return string
	 */
	public static function getPathService($p_s_serviceName)
	{
		return ServicesManagement::C_S_DIRECTORY_SERVICES.$p_s_serviceName.".php";
	}
}

/**
 * Interface for Service Class
 * @author PoLuX
 */
interface InternalService {
	/**
	 *  Return true if mand. parameters specificied in the procedure call
	 * @param array(key,value) $p_as_parameters  HTTP query parameters 
	 * @return boolean
	 */
	public function checkParametersNeeded($p_as_parameters);
}

/**
 * Abstract class about a service
 * @author PoLuX
 */
abstract class AService implements InternalService {
	/**
	 * Service HTTP parameters needed
	 * @var array(string)
	 * @example array('temp','pres','hum','lum')
	 */
	protected $_as_parameters = array();
	/**
	 * Service HTTP parameters optional
	 * @var array(string)
	 * @example array('ID_Culture')
	 */
	protected $_as_optParams = array();
	/**
	 * ResultType of Request about a service
	 * @var string
	 * @example EXCEPTION,NO_DATA,OK
	 */	
	protected $_s_codeRequestResult = null;
	/**
	 * Data result for Response
	 * @var array(mixed)
	 */
	protected $_ax_dataResult = array();
	/**
	 * Output format
	 * @var string
	 * @example json,xml,html
	 */
	protected $_s_format = "json";
	/**
	 * Exception (if any)
	 * @var Exception
	 */
	protected $_o_exception=null;
	/**
	 * Message for query
	 * @var array(string)
	 */
	protected $_as_message=array();
	/**
	 * Return true if mand. parameters specificied in the procedure call
	 * (non-PHPdoc)
	 *
	 * @see InternalService::checkParametersNeeded()
	 * @final
	 * @param array(key,value) $p_as_parameters  HTTP query parameters 
	 * @return boolean
	*/
	final function checkParametersNeeded($p_as_parameters)
	{
		$l_b_result = true;

		if($this->_as_parameters!=0)
			foreach( $this->_as_parameters as $l_s_key )
			{
				if(!$l_b_result)
				{
					$l_b_result = false;
				}
				else
				{
					$l_b_result = $l_b_result && array_key_exists($l_s_key, $p_as_parameters);
				}
			}
		else
			$l_b_result = false;

		return $l_b_result;
	}
	
	
	/**
	 * Format data for response
	 * @final
	 */
	final public function getRequestOutput()
	{
		 $l_s_result = "";
		 $l_ax_result = array();
		 
		 $l_ax_result['code'] = $this->_s_codeRequestResult;
		 $l_ax_result['msgs'] = $this->_as_message;
		 
		 if($this->_s_codeRequestResult == "OK")
		 	$l_ax_result['data'] = $this->_ax_dataResult;
		
		 if($this->_s_codeRequestResult == "EXCEPTION")
		 	$l_ax_result['exceptionMsg'] = $this->_o_exception->getMessage();
		
		
		 
		 
		 switch($this->_s_format)
		 {
		 	case "json":
		 		$l_s_result = json_encode($l_ax_result);
		 	//	echo json_last_error();
		 	//	print_r($l_s_result);
		 		break;
		 	case "xml":
		 		$l_s_result = print_r($l_ax_result,true);
		 		break;
		 	case "html":
	 			$l_s_result = html_entity_decode($l_ax_result);
	 			break;
	 	 	default:
		 		$l_s_result = print_r($l_ax_result,true);
		 		break;
		 }
		 
		 return $l_s_result;
	}
}

/**
 * Abstract class about adding data into DB service
 * 
 * @author PoLuX
 *
 */
abstract class AServiceAddInfos extends AService{
	
	/**
	 * Execution of service instance
	 * 
	 * @param array(mixed) $p_as_parameters Data
	 * @abstract
	 * @access public
	 */
	abstract public function executeService($p_as_parameters);
}

/**
 * Abstract class about getting data from DB service
 *
 * @author PoLuX
 *
 */
abstract class AServiceGetInfos extends AService {

	 /**
	  * Service Output data definition
	  * @var array(mixed) Columns' name enumeration
	  */
	 protected $_ax_outputDataDef = array();
	 
	
	 /**
	  * Récupération des données
	  * @param array(string) $p_as_OutputParam
	  */
	 abstract public function getData($p_as_OutputParam);
}

// End of File
